﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Events;
using BBInterfaceNET.Events;
using BaseTypesLib.Model;

namespace BBInterfaceNET.PropertiesWindow.ViewModels
{
    public class PropertiesViewModel:NotificationObject
    {
        IEventAggregator eventAggregator;

        public PropertiesViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            FieldSelectedEvent evt = eventAggregator.GetEvent<FieldSelectedEvent>();
            evt.Subscribe(OnSelectedFieldChanged, ThreadOption.UIThread);
            
        }
        public void RaiseFieldPropertyChanged(object newValue, object oldValue)
        {
            FieldChangedEvent evt = eventAggregator.GetEvent<FieldChangedEvent>();
            evt.Publish(new Events.Model.FieldChangedData() { NewValue = newValue, OldValue = oldValue });
        }
        public void OnSelectedFieldChanged(Field selField)
        {
            //call ToList() to make a new list with the same elements
            SelectedField = selField;
        }
        public Field SelectedField
        {
            get { return selField; }
            set
            {
                if (selField != value)
                {
                    selField = value;
                    RaisePropertyChanged(() => SelectedField);
                }
            }
        }
        private Field selField;
    }
}
